//
//  DBManager.m
//  BaseLib
//
//  Created by CPX on 2018/5/28.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "DBManager.h"

#define DBVersionKey @"DBVersion"
#define DBVersion 1

/**
 数据库管理
 @author yzl
 */
@implementation DBManager
{
    FMDatabase *_db;
}

+ (instancetype)sharedManager{
    static DBManager *mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [DBManager new];
    });
    return mgr;
}

- (void)initWithMigration:(MigrationBlock)block{
    
    
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 文件路径
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"muslim.db"];
    
    NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"muslim" ofType:@"db"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:filePath error:&error];
        if (error) {
            NSLog(@"-------db error-----");
            NSLog(@"%@",error);
        }
        _db = [FMDatabase databaseWithPath:filePath];
        [[NSUserDefaults standardUserDefaults] setObject:@(DBVersion) forKey:DBVersionKey];
    }
    else if([[NSUserDefaults standardUserDefaults] integerForKey:DBVersionKey] != DBVersion){
        _db = [FMDatabase databaseWithPath:filePath];
        NSDictionary *dic = nil;
        if (block) {
            dic = block();
        }
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        NSError *error = nil;
        [[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:filePath error:&error];
        if (error) {
            NSLog(@"-------db error-----");
            NSLog(@"%@",error);
        }
        _db = [FMDatabase databaseWithPath:filePath];
        [_db open];
        
        NSArray *a1 = dic[@"quran_favorites"];
        NSMutableString *sql = [NSMutableString string];
        for (NSDictionary *d in a1) {
            NSString *ggg = [NSString stringWithFormat:@"insert into quran_favorites values(%@,%@);",d[@"sura_number"],d[@"aya_number"]];
            [sql appendString:ggg];
        }
        
        a1 = dic[@"quran_notes"];
        for (NSDictionary *d in a1) {
            NSString *ggg = [NSString stringWithFormat:@"insert into quran_notes values(%@,%@,\"%@\");",d[@"sura_number"],d[@"aya_number"],d[@"content"]];
            [sql appendString:ggg];
        }
        
        a1 = dic[@"quran_read"];
        for (NSDictionary *d in a1) {
            NSString *ggg = [NSString stringWithFormat:@"insert into quran_read values(%@,%@);",d[@"sura_number"],d[@"aya_number"]];
            [sql appendString:ggg];
        }
        
//        [_db beginTransaction];
        
        [_db executeStatements:sql];
        
        [_db close];
        [[NSUserDefaults standardUserDefaults] setObject:@(DBVersion) forKey:DBVersionKey];
    }
    else{
        _db = [FMDatabase databaseWithPath:filePath];
    }
    
    // 实例化FMDataBase对象
    
//    _db = [FMDatabase databaseWithPath:filePath];
}

- (void)open{
    [_db open];
}

- (void)close{
    [_db close];
}

@end
